﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Register.Model;
using Register.BLL;
using System.Web.Script.Serialization;
using Register.Command;

namespace RegisterWeb.Manager.AnnouncementManager.ashx
{
    /// <summary>
    /// AnnouncementManagerService 的摘要说明
    /// </summary>
    public class AnnouncementManagerService : IHttpHandler
    {

         public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            //context.Response.Write("Hello World");

            String action = context.Request["action"].ToString();
            if (action.Equals("list"))
            {
                int row = int.Parse(context.Request["rows"].ToString());
                int page = int.Parse(context.Request["page"].ToString());
                List<Announcement> list = new List<Announcement>();
                list = AnnouncementBLL.GetPagedAnnouncement(row, page, " Announcement_Status=1 ");

                List<AnnouncementJSON> json = new List<AnnouncementJSON>();
                foreach (Announcement announcement in list)
                {
                    json.Add(new AnnouncementJSON(announcement.Announcement_Title, announcement.Announcement_ID, announcement.Announcement_Content, announcement.Announcement_NumberViews.ToString(), announcement.Announcement_AddTime.ToString(), announcement.Announcement_Recoder, announcement.Announcement_Status));
                }

                AnnouncementListJSON announcementListJSON = new AnnouncementListJSON();
                announcementListJSON.total = AnnouncementBLL.GetAnnouncementCount(null);
                announcementListJSON.rows = json;

                JavaScriptSerializer jss = new JavaScriptSerializer();
                context.Response.Write(jss.Serialize(announcementListJSON));
            }
            else if (action.Equals("delete"))
            {
                String ID = context.Request["id"];
                if (AnnouncementBLL.DeleteAnnouncement(ID))
                    context.Response.Write("ok");
                else
                    context.Response.Write("no");
            }
            else if (action.Equals("add"))
            {
                Announcement announcement = new Announcement();
                announcement.Announcement_ID = DataIDHelper.GetDataID("Announcement_ID");
                announcement.Announcement_Title = context.Request["Title"];
                announcement.Announcement_Content = context.Request["Content"];
                announcement.Announcement_Recoder = "admin";///////////////////////////////////////////////////////////////////////////记录
                announcement.Announcement_AddTime = DateTime.Now;
                announcement.Announcement_NumberViews = 0;
                announcement.Announcement_Status = "1";

                //if (AdminInfoBLL.AddAdminInfo(admin))
                //    context.Response.Write("ok");
                //else
                //    context.Response.Write("no");
                JavaScriptSerializer jss = new JavaScriptSerializer();
                context.Response.Write(jss.Serialize(new message(AnnouncementBLL.AddAnnouncement(announcement), "公告添加成功！")));

            }
            else if (action.Equals("edit"))
            {
                Announcement announcement = new Announcement();
                announcement.Announcement_ID = context.Request["id"];
                announcement.Announcement_Title = context.Request["Title"];
                announcement.Announcement_Content = context.Request["Content"];
                JavaScriptSerializer jss = new JavaScriptSerializer();
                context.Response.Write(jss.Serialize(new message(AnnouncementBLL.UpdateAnnouncement(announcement), "编辑公告成功！")));

            }
            else if (action.Equals("check"))
            {
                string name = context.Request["Title"];
                if (AdminInfoBLL.Exists(name))
                {
                    context.Response.Write("ok");
                }
                else
                {
                    context.Response.Write("no");
                }
            }

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }

    class message
    {
        public message(bool success, string msg)
        {
            this.msg = msg;
            this.success = success;
        }
        public bool success { get; set; }
        public string msg { get; set; }
    }

    class AnnouncementListJSON
    {

        public int total { get; set; }
        public List<AnnouncementJSON> rows { get; set; }

    }

    class AnnouncementJSON
    {
        public String Title { get; set; }
        public String ID { get; set; }
        public String Content { get; set; }
        public String NumberViews { get; set; }
        public String AddTime { get; set; }
        public String Recoder { get; set; }
        public String Status { get; set; }

        public AnnouncementJSON(String Title, String ID, String Content, String NumberViews, String AddTime, String Recoder, String Status)
        {
            this.Title = Title;
            this.ID = ID;
            this.Content = Content;
            this.NumberViews = NumberViews;
            this.AddTime = AddTime;
            this.Recoder = Recoder;
            this.Status = Status;
        }

    }
}